﻿<%@ Page Language="C#" %>
<%try{
                               Response.ContentType = "text/xml";
                               double minx = 120.13 - 0.13;
                               double maxx = 120.13 + 0.13;
                               double miny = 30.267 - 0.13;
                               double maxy = 30.267 + 0.13;
                               string style = Request.Params["style"];
                               if (Request.Params["minx"] != null)
                               {
                                   minx = double.Parse(Request.Params["minx"]);
                                   miny = double.Parse(Request.Params["miny"]);
                                   maxy = double.Parse(Request.Params["maxy"]);
                                   maxx = double.Parse(Request.Params["maxx"]);
                               }
                               double width = (maxx - minx)/2;
                               double height = (maxy - miny)/2;
                               string host = Request.Url.Host;
                               string port = Request.Url.Port.ToString();
                               string path = Request.ApplicationPath;
                               if (port == "80")
                               {
                                   port = "";
                               }
                               else
                               {
                                   port = ":" + port;
                               }
                               string url = "http://" + host + port  + path + "/";
                               string format = "image%2Fpng";
                               if (width * 2 < 0.005) 
                               {
                                   format = "kml";
                               }
                               string kmlurl = "http://localhost:8080/geoserver/wms?LAYERS=csxy%3AJZ&STYLES=" + style + "kml&HEIGHT=256&WIDTH=256&SRS=EPSG%3A4326&FORMAT=" + format + "&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&BBOX=" + minx + "," + miny + "," + maxx + "," + maxy + "&TRANSPARENT=TRUE";
     %><?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>

<%if (Request.Params["minx"] == null)
  { %>
<Region>
        <LatLonAltBox>
          <north><%=maxy%></north><south><%=miny%></south>
          <east><%=maxx%></east><west><%=minx%></west>
        </LatLonAltBox>
      </Region>
<ScreenOverlay>
    <name>Absolute Positioning: Top left</name>
    <Icon>
      <href>legend1/<%=style.Replace("kml", "")%>.png</href>
    </Icon>
    <visibility>1</visibility>  
    <overlayXY x="0" y="1" xunits="fraction" yunits="fraction"/>
    <screenXY x="0" y="1" xunits="fraction" yunits="fraction"/>
    <rotationXY x="0" y="0" xunits="fraction" yunits="fraction"/>
    <size x="0" y="0" xunits="fraction" yunits="fraction"/>
  </ScreenOverlay>
  <NetworkLink>
      <name>001120</name>
      <Region>
        <Lod>
          <minLodPixels>256</minLodPixels><maxLodPixels>-1</maxLodPixels>
        </Lod>
        <LatLonAltBox>
          <north><%=maxy%></north><south><%=miny%></south>
          <east><%=maxx%></east><west><%=minx%></west>
        </LatLonAltBox>
      </Region>
      <Link>
        <href><%=url%>kml.aspx?minx=<%=minx%>&amp;maxx=<%=maxx%>&amp;miny=<%=miny%>&amp;maxy=<%=maxy%>&amp;style=<%=style%></href>
        <viewRefreshMode>onRegion</viewRefreshMode>
      </Link>
    </NetworkLink>
  </Document>
  </kml>
  <%
    
  }
  else
  {
                                %>
    <Region>
      <Lod>
        <minLodPixels>256</minLodPixels><maxLodPixels>512</maxLodPixels>
      </Lod>
      <LatLonAltBox>
        <north><%=maxy%></north><south><%=miny%></south>
        <east><%=maxx%></east><west><%=minx%></west>
      </LatLonAltBox>
    </Region>
    <NetworkLink>
      <name>001120</name>
      <Region>
        <Lod>
          <minLodPixels>256</minLodPixels><maxLodPixels>-1</maxLodPixels>
        </Lod>
        <LatLonAltBox>
          <north><%=maxy%></north><south><%=maxy - height%></south>
          <east><%=minx + width%></east><west><%=minx%></west>
        </LatLonAltBox>
      </Region>
      <Link>
        <href><%=url%>kml.aspx?minx=<%=minx%>&amp;maxx=<%=minx + width%>&amp;miny=<%=maxy - height%>&amp;maxy=<%=maxy%>&amp;style=<%=style%></href>
        <viewRefreshMode>onRegion</viewRefreshMode>
      </Link>
    </NetworkLink>
        <NetworkLink>
      <name>001120</name>
      <Region>
        <Lod>
          <minLodPixels>256</minLodPixels><maxLodPixels>-1</maxLodPixels>
        </Lod>
        <LatLonAltBox>
          <north><%=maxy%></north><south><%=maxy - height%></south>
          <east><%=maxx%></east><west><%=maxx - width%></west>
        </LatLonAltBox>
      </Region>
      <Link>
        <href><%=url%>kml.aspx?minx=<%=minx + width%>&amp;maxx=<%=maxx%>&amp;miny=<%=maxy - height%>&amp;maxy=<%=maxy%>&amp;style=<%=style%></href>
        <viewRefreshMode>onRegion</viewRefreshMode>
      </Link>
    </NetworkLink>
        <NetworkLink>
      <name>001120</name>
      <Region>
        <Lod>
          <minLodPixels>256</minLodPixels><maxLodPixels>-1</maxLodPixels>
        </Lod>
        <LatLonAltBox>
          <north><%=maxy - height%></north><south><%=miny%></south>
          <east><%=minx + width%></east><west><%=minx%></west>
        </LatLonAltBox>
      </Region>
      <Link>
        <href><%=url%>kml.aspx?minx=<%=minx%>&amp;maxx=<%=minx + width%>&amp;miny=<%=miny%>&amp;maxy=<%=maxy - height%>&amp;style=<%=style%></href>
        <viewRefreshMode>onRegion</viewRefreshMode>
      </Link>
    </NetworkLink>
        <NetworkLink>
      <name>001120</name>
      <Region>
        <Lod>
          <minLodPixels>256</minLodPixels><maxLodPixels>-1</maxLodPixels>
        </Lod>
        <LatLonAltBox>
          <north><%=maxy - height%></north><south><%=miny%></south>
          <east><%=maxx%></east><west><%=maxx - width%></west>
        </LatLonAltBox>
      </Region>
      <Link>
        <href><%=url%>kml.aspx?minx=<%=minx + width%>&amp;maxx=<%=maxx%>&amp;miny=<%=miny%>&amp;maxy=<%=maxy - height%>&amp;style=<%=style%></href>
        <viewRefreshMode>onRegion</viewRefreshMode>
      </Link>
    </NetworkLink>
    <%if (format != "kml")
      { %>
    <GroundOverlay>
      <drawOrder>5</drawOrder>
      <Icon>
        <href>http://<%=host%>:8080/geoserver/wms?LAYERS=csxy%3AJZ&amp;STYLES=<%=style%>&amp;HEIGHT=256&amp;WIDTH=256&amp;SRS=EPSG%3A4326&amp;FORMAT=<%=format%>&amp;SERVICE=WMS&amp;VERSION=1.1.1&amp;REQUEST=GetMap&amp;EXCEPTIONS=application%2Fvnd.ogc.se_inimage&amp;BBOX=<%=minx%>,<%=miny%>,<%=maxx%>,<%=maxy%>&amp;TRANSPARENT=TRUE</href>
      </Icon>
      <LatLonBox>
        <north><%=maxy%></north><south><%=miny%></south>
        <east><%=maxx%></east><west><%=minx%></west>
      </LatLonBox>
    </GroundOverlay>
    <%}
      else
      { %>
      <NetworkLink>
      <Link>
      <href><%=url%>kmlproxy.aspx?endpoint=<%=Server.UrlEncode(kmlurl)%>
      </href>
      </Link>
      </NetworkLink>
      <%} %>
    </Document>
</kml>
<%}
                           }
                           catch (Exception ex)
                           {%><%=ex.Message %><%}%>